// 怪物系统
library MonsterVo initializer init requires GameData

    globals
        // 将所有的怪物都放到table 父key=玩家id
        // 子key = 1获取存储怪物的group
        // 子key = 2获取存储怪物的数量
        // 子key = 3存储距离英雄最近的怪物
        // 子key = 4存储单位组中的随机一个单位
        // 子key = 5进入怪物池子的怪物数量，累计
        hashtable MONSTER_TABLE = InitHashtable()
        // 各自玩家的怪物数量
        integer array MONSTER_COUNT
        
        integer array MONSTER_TIMER_YS
        integer array MONSTER_TIMER_YS_TIME
    endglobals

    // 获取指定玩家的怪物池子
    function getMonsterGroup takes integer uid returns group
        return LoadGroupHandle(MONSTER_TABLE, uid, 1)
    endfunction

    // 添加怪物到hashtable
    function addMonsterToTable takes integer uid, unit u returns nothing
        call GroupAddUnit(getMonsterGroup(uid), u)
        call SaveInteger(MONSTER_TABLE, uid, 2, LoadInteger(MONSTER_TABLE, uid, 2) + 1)
        call SaveInteger(MONSTER_TABLE, uid, 5, LoadInteger(MONSTER_TABLE, uid, 5) + 1)
    endfunction

    // 从hashtable移除怪物(怪物死亡后要移除)
    function removeMonsterFromTable takes integer uid, unit u returns nothing
        set uid = FF_GetUnitOwner(u)
        call GroupRemoveUnit(getMonsterGroup(uid), u)
        call SaveInteger(MONSTER_TABLE, uid, 2, LoadInteger(MONSTER_TABLE, uid, 2) - 1)
    endfunction

    // 获取指定玩家的怪物数量
    function getMonsterCount takes integer uid returns integer
        return LoadInteger(MONSTER_TABLE, uid, 2)
    endfunction

    // 获取所有玩家的怪物数量
    function getMonsterCountAll takes nothing returns integer
        local integer count = 0
        local integer uid = 0
        loop
            exitwhen uid > PLAYER_NUMBER
            set count = count + getMonsterCount(uid)
            set uid = uid + 1
        endloop

        return count
    endfunction

    function forg_nearunit takes nothing returns nothing
        local integer uid = FF_GetUnitOwner(GetEnumUnit())
        local real x = GetUnitX(udg_player_select_hero[uid])
        local real y = GetUnitY(udg_player_select_hero[uid])
        local real ex = GetUnitX(GetEnumUnit())
        local real ey = GetUnitY(GetEnumUnit())

        if getDistance(x, y, ex, ey) <= getDistance(x, y, GetUnitX(LoadUnitHandle(MONSTER_TABLE, uid, 3)), GetUnitY(LoadUnitHandle(MONSTER_TABLE, uid, 3))) then
            call SaveUnitHandle(MONSTER_TABLE, uid, 3, GetEnumUnit())
        endif

        // call BJDebugMsg("name : " + GetUnitName(GetEnumUnit()))
    endfunction
    // 获得距离英雄最近的单位，也就是要被攻击的单位
    function getNearestUnit takes integer uid returns unit
        // TODO 如果英雄在BOSS区域获取最近的单位需要重写，这里的group需要换成Boss区域group，遍历需要重写
        call SaveUnitHandle(MONSTER_TABLE, uid, 3, FirstOfGroup(getMonsterGroup(uid)))
        call ForGroup(getMonsterGroup(uid), function forg_nearunit)

        // return FirstOfGroup(getMonsterGroup(uid))  这里不行，单位组结构师站结构
        return LoadUnitHandle(MONSTER_TABLE, uid, 3)
    endfunction

    function getRandomUnitEnum takes nothing returns nothing
        local integer uid = FF_GetUnitOwner(GetEnumUnit())

        call SaveUnitHandle(MONSTER_TABLE, uid, 4, GetEnumUnit())
        if GetRandomInt(1, 2) == 1 then
            return 
        endif
    endfunction
    // 获取怪物池中随机一个单位
    function getRandomUnit takes integer uid returns unit
        // TODO 如果英雄在BOSS区域获取最近的单位需要重写，这里的group需要换成Boss区域group，遍历需要重写
        call SaveUnitHandle(MONSTER_TABLE, uid, 4, FirstOfGroup(getMonsterGroup(uid)))
        call ForGroup(getMonsterGroup(uid), function getRandomUnitEnum)

        return LoadUnitHandle(MONSTER_TABLE, uid, 4)
    endfunction

    function timer_createMons takes nothing returns nothing
        local real x
        local real y
        local unit u

        local real ydSpeed = 100.0
        local real hp = 100.0
        local integer mdli
        local string mdl = ""

        local integer uid = 0
        loop
            exitwhen uid > PLAYER_NUMBER
            // 判断玩家是否在线
            if isPlayerOnline(uid) then
            // if 1 == 1 then

                // call BJDebugMsg("uid = " + I2S(uid))

                set MONSTER_TIMER_YS[uid] = MONSTER_TIMER_YS[uid] + 1
                if MONSTER_TIMER_YS[uid] >= MONSTER_TIMER_YS_TIME[uid] then

                    set MONSTER_TIMER_YS[uid] = 0

                    set x = GetRectRandomX(getMonsterCreateRect(uid))
                    set y = GetRectRandomY(getMonsterCreateRect(uid))

                    if ModuloInteger(LoadInteger(MONSTER_TABLE, uid, 5), 50) == 0 then
                        set hp = hp + 100.0
                    endif
                    if LoadInteger(MONSTER_TABLE, uid, 5) == 50 then
                        set MONSTER_TIMER_YS_TIME[uid] = 8
                    endif
                    

                    if LoadInteger(MONSTER_TABLE, uid, 5) == 100 then
                        set ydSpeed = 200.0
                        set MONSTER_TIMER_YS_TIME[uid] = 5
                    endif

                    if LoadInteger(MONSTER_TABLE, uid, 5) == 200 then
                        set MONSTER_TIMER_YS_TIME[uid] = 3
                    endif

                    if LoadInteger(MONSTER_TABLE, uid, 5) > 100 then
                        set mdli = GetRandomInt(1, 3)

                        set mdl = "xiaoguai" + I2S(mdli) + ".mdl"

                        if mdli == 1 then
                            set hp = hp * 1.5
                        endif

                        if mdli == 3 then
                            set ydSpeed = 300.0
                        endif
                    endif

                    set u = createUByProperty('hkni', 10001, 0, "名字", x, y, 270.0, 100, hp, 100, 100, 100, 100, 100, 100, 100, 100, mdl, 1, ydSpeed)
                    // 设置所属英雄的怪物
                    call FF_SetUnitOwner(u, uid)
                    call IssuePointOrder( u, "move",  GetRectCenterX(getPlayerStartRect(uid)), GetRectCenterY(getPlayerStartRect(uid)))
                    call addMonsterToTable(uid, u)
                endif
            endif

            set uid = uid + 1
        endloop

        set u = null
    endfunction

    // 刷怪
    private function createMons takes nothing returns nothing

        local timer t = CreateTimer()
        call TimerStart(t, 0.1, true, function timer_createMons)
    endfunction

    private function init takes nothing returns nothing
        local integer uid = 0

        loop
            exitwhen uid > PLAYER_NUMBER
            set MONSTER_TIMER_YS_TIME[uid] = 10
            // 保存初始怪物池子
            call SaveGroupHandle(MONSTER_TABLE, 0, 1, CreateGroup())    
            set uid = uid + 1
        endloop

        // 创建怪物
        call createMons()
    endfunction
endlibrary


